package com.cargo.base.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Title: DateUtil.java
 * @author czz
 * @date 2016年1月22日
 * @version V1.0
 */
public class DateUtil {
	
	public static final String yyyyMMddSL = "yyyy/MM/dd";
	
	public static final String yyyyMMddHHmmssSL = "yyyy/MM/dd HH:mm:ss";
	
	public static final String yyyyMMddHY = "yyyy-MM-dd";
	
	public static final String yyyyMMddHHmmssHY = "yyyy-MM-dd HH:mm:ss";
	
	public static final String yyyyMMddHHmm = "yyyy-MM-dd HH:mm";
	
	public static final String MMdd = "MM/dd";
	
	public static Date format(String date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return format.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static Date formatNew(String date,String format) {
		if(!BaseVerifyUtil.isNotEmpty(format)){
			format = "yyyy-MM-dd";
		}
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		try {
			return simpleDateFormat.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static Date newFormat(String date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
		try {
			return format.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 转换日期格式
	 * @param date
	 * @param format
	 * @return
	 */
	public static String formatDateToString( Date date, String format ) {
		    if ( date == null ) {
		      return "";
		    }
		    String targetFormat = format;
		    if (StringVerifyUtil.isEmpty( format ) ) {
		      targetFormat = "yyyyMMdd";
		    }
		    SimpleDateFormat dateFormat = new SimpleDateFormat( targetFormat );
		    return dateFormat.format( date );
	}
	
	/**
	 * 将获取到的字符规范成规定格式
	 * @param week
	 * @return
	 */
	public static String getWeek(String week){		
		String strWeek="";
		if(week==null || "".equals(week)){
			return "";
		}else if(week.equalsIgnoreCase("Sun") || week.equalsIgnoreCase("Sunday") || week.matches("周日|日|7")){
			return "周日";
		}else if(week.equalsIgnoreCase("Mon") || week.equalsIgnoreCase("Monday") || week.matches("周一|一|1")){
			return "周一";
		}else if(week.equalsIgnoreCase("Tue") || week.equalsIgnoreCase("Tuesday") || week.matches("周二|二|2")){
			return "周二";
		}else if(week.equalsIgnoreCase("Wed") || week.equalsIgnoreCase("Wednesday") || week.matches("周三|三|3")){
			return "周三";
		}else if(week.equalsIgnoreCase("Thu") || week.equalsIgnoreCase("Thursday") || week.matches("周四|四|4")){
			return "周四";
		}else if(week.equalsIgnoreCase("Fri") || week.equalsIgnoreCase("Friday") || week.matches("周五|五|5")){
			return "周五";
		}else if(week.equalsIgnoreCase("Sat") || week.equalsIgnoreCase("Saturday") || week.matches("周六|六|6")){
			return "周六";
		}
		return strWeek;
	}
	
	/**
	 * 转换日期
	 * @param week
	 * @return
	 */
	public static String getWeekNew(String week){		
		String strWeek="";
		if(week==null || "".equals(week)){
			return "";
		} 
		if(week.indexOf("1") > -1 || week.indexOf("一") > -1 || week.indexOf("Mon") > -1 || week.indexOf("Monday") > -1 || week.indexOf("周一") > -1){
			strWeek = "周一" + ",";
		}
		if(week.indexOf("2") > -1 || week.indexOf("二") > -1 || week.indexOf("Tue") > -1 || week.indexOf("Tuesday") > -1 || week.indexOf("周二") > -1){
			strWeek += "周二" + ",";
		}
		if(week.indexOf("3") > -1 || week.indexOf("三") > -1 || week.indexOf("Wed") > -1 || week.indexOf("Wednesday") > -1 || week.indexOf("周三") > -1){
			strWeek += "周三" + ",";
		}
		if(week.indexOf("4") > -1 || week.indexOf("四") > -1 || week.indexOf("Thu") > -1 || week.indexOf("Thursday") > -1 || week.indexOf("周四") > -1){
			strWeek += "周四" + ",";
		}
		if(week.indexOf("5") > -1 || week.indexOf("五") > -1 || week.indexOf("Fri") > -1 || week.indexOf("Friday") > -1 || week.indexOf("周五") > -1){
			strWeek += "周五" + ",";
		}
		if(week.indexOf("6") > -1 || week.indexOf("六") > -1 || week.indexOf("Sat") > -1 || week.indexOf("Saturday") > -1 || week.indexOf("周六") > -1){
			strWeek += "周六" + ",";
		}
		if(week.indexOf("7") > -1 || week.indexOf("日") > -1 || week.indexOf("Sun") > -1 || week.indexOf("Sunday") > -1 || week.indexOf("周日") > -1){
			strWeek += "周日";
		} 
		if(StringVerifyUtil.isNotEmpty(strWeek)){
			if(strWeek.substring(strWeek.length()-1, strWeek.length()).equals(",")){
				strWeek = strWeek.substring(0, strWeek.length() - 1);
			}
		}
		return strWeek;
	}
	
	
	
	/**
	 * 转换日期
	 * @param week
	 * @return
	 */
	public static String getWeekCN(String week){		
		String strWeek="";
		if(week==null || "".equals(week)){
			return "";
		} 
		if(week.indexOf("1") > -1 || week.indexOf("一") > -1 || week.indexOf("Mon") > -1 || week.indexOf("Monday") > -1 || week.indexOf("周一") > -1){
			strWeek = "一" + ",";
		}
		if(week.indexOf("2") > -1 || week.indexOf("二") > -1 || week.indexOf("Tue") > -1 || week.indexOf("Tuesday") > -1 || week.indexOf("周二") > -1){
			strWeek += "二" + ",";
		}
		if(week.indexOf("3") > -1 || week.indexOf("三") > -1 || week.indexOf("Wed") > -1 || week.indexOf("Wednesday") > -1 || week.indexOf("周三") > -1){
			strWeek += "三" + ",";
		}
		if(week.indexOf("4") > -1 || week.indexOf("四") > -1 || week.indexOf("Thu") > -1 || week.indexOf("Thursday") > -1 || week.indexOf("周四") > -1){
			strWeek += "四" + ",";
		}
		if(week.indexOf("5") > -1 || week.indexOf("五") > -1 || week.indexOf("Fri") > -1 || week.indexOf("Friday") > -1 || week.indexOf("周五") > -1){
			strWeek += "五" + ",";
		}
		if(week.indexOf("6") > -1 || week.indexOf("六") > -1 || week.indexOf("Sat") > -1 || week.indexOf("Saturday") > -1 || week.indexOf("周六") > -1){
			strWeek += "六" + ",";
		}
		if(week.indexOf("7") > -1 || week.indexOf("日") > -1 || week.indexOf("Sun") > -1 || week.indexOf("Sunday") > -1 || week.indexOf("周日") > -1){
			strWeek += "日";
		} 
		if(StringVerifyUtil.isNotEmpty(strWeek)){
			if(strWeek.substring(strWeek.length()-1, strWeek.length()).equals(",")){
				strWeek = strWeek.substring(0, strWeek.length() - 1);
			}
		}
		return strWeek;
	}
	
	
	/**
	 * 转换日期
	 * @param week
	 * @return
	 */
	public static String getWeekNum(String week){		
		String strWeek="";
		if(week==null || "".equals(week)){
			return "";
		} 
		if(week.indexOf("1") > -1 || week.indexOf("一") > -1 || week.indexOf("Mon") > -1 || week.indexOf("Monday") > -1 || week.indexOf("周一") > -1){
			strWeek = "1" + ",";
		}
		if(week.indexOf("2") > -1 || week.indexOf("二") > -1 || week.indexOf("Tue") > -1 || week.indexOf("Tuesday") > -1 || week.indexOf("周二") > -1){
			strWeek += "2" + ",";
		}
		if(week.indexOf("3") > -1 || week.indexOf("三") > -1 || week.indexOf("Wed") > -1 || week.indexOf("Wednesday") > -1 || week.indexOf("周三") > -1){
			strWeek += "3" + ",";
		}
		if(week.indexOf("4") > -1 || week.indexOf("四") > -1 || week.indexOf("Thu") > -1 || week.indexOf("Thursday") > -1 || week.indexOf("周四") > -1){
			strWeek += "4" + ",";
		}
		if(week.indexOf("5") > -1 || week.indexOf("五") > -1 || week.indexOf("Fri") > -1 || week.indexOf("Friday") > -1 || week.indexOf("周五") > -1){
			strWeek += "5" + ",";
		}
		if(week.indexOf("6") > -1 || week.indexOf("六") > -1 || week.indexOf("Sat") > -1 || week.indexOf("Saturday") > -1 || week.indexOf("周六") > -1){
			strWeek += "6" + ",";
		}
		if(week.indexOf("7") > -1 || week.indexOf("日") > -1 || week.indexOf("Sun") > -1 || week.indexOf("Sunday") > -1 || week.indexOf("周日") > -1){
			strWeek += "7";
		} 
		if(StringVerifyUtil.isNotEmpty(strWeek)){
			if(strWeek.substring(strWeek.length()-1, strWeek.length()).equals(",")){
				strWeek = strWeek.substring(0, strWeek.length() - 1);
			}
		}
		return strWeek;
	}
	
	/**
	 * getTodayStart:(获取一天的开始时间).
	 * @return 
	 * @return :Date 
	 * @Creator:wizard
	 * @Date:2016年5月13日 上午11:56:46
	 */
	public static Date getTodayStart(){
		Calendar currentDate = new GregorianCalendar();   
		currentDate.set(Calendar.HOUR_OF_DAY, 0);  
		currentDate.set(Calendar.MINUTE, 0);  
		currentDate.set(Calendar.SECOND, 0);
		return (Date)currentDate.getTime().clone();
	}
	
	/**
	 * getTodayEnd:(获取一天的结束时间).
	 * @return 
	 * @return :Date 
	 * @Creator:wizard
	 * @Date:2016年5月13日 上午11:57:57
	 */
	public static Date getTodayEnd(){
		Calendar currentDate = new GregorianCalendar();   
		currentDate.set(Calendar.HOUR_OF_DAY, 23);  
		currentDate.set(Calendar.MINUTE, 59);  
		currentDate.set(Calendar.SECOND, 59);  
		return (Date)currentDate.getTime().clone();  
	}
	
	
	/**
	 * 
	 * checkDate:验证字符串格式的日期的合法性
	 * @param date
	 * @return 
	 * @return :boolean 
	 * @Creator:wizard
	 * @Date:2016年6月6日 上午10:32:06
	 */
	public static boolean checkDate(String date){
		/**
		 * 判断日期格式和范围
		 */
		String rexp = "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))";

		Pattern pat = Pattern.compile(rexp);  

		Matcher mat = pat.matcher(date);  

		boolean dateType = mat.matches();

		return dateType;
	}
	
	/**
	 * 
	 * getbeforeDate:获取日期
	 * @return 
	 * @return :String 
	 * @Creator:wizard
	 * @Date:2016年7月18日 上午10:43:52
	 */
	public static String getDate(String format,int num){
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, num); 
		Date date = calendar.getTime();
		DateFormat df = new SimpleDateFormat(format);
		return df.format(date);
	}
	
	
	/**
	 * 
	 * printBeforDays:获取前N天所有日期
	 * @param index 获取前几天
	 * @param num   循环数字
	 * @return 
	 * @return :List<String> 
	 * @Creator:wizard
	 * @Date:2016年7月18日 下午5:18:18
	 */
	public static List<String> printBeforDays(int index, int num) {
		List<String> dayList = new ArrayList<>();
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DATE, index);
		for (int i = 0; i < num; i++) {
			String day = DateUtil.formatDateToString(c.getTime(), "yyyy-MM-dd");
			dayList.add(day);
			c.add(Calendar.DATE, 1);
		}
		return dayList;
	}
	
	
	
	/**
	 * 
	 * printBeforDays:获取指定日前的前几天
	 * @param index 获取前几天
	 * @param num   循环数字
	 * @return 
	 * @return :List<String> 
	 * @Creator:wizard
	 * @Date:2016年7月18日 下午5:18:18
	 */
	public static List<String> printBeforDays(int index, int num,String date) {
		List<String> dayList = new ArrayList<>();
		Calendar c = Calendar.getInstance();
		c.setTime(DateUtil.format(date));
		c.add(Calendar.DATE, index);
		for (int i = 0; i < num; i++) {
			String day = DateUtil.formatDateToString(c.getTime(), "yyyy-MM-dd");
			dayList.add(day);
			c.add(Calendar.DATE, 1);
		}
		return dayList;
	}
	
	/**
	 * 
	 * checkTwoMonth:判断是否跨月
	 * @return 
	 * @return :boolean 
	 * @Creator:wizard
	 * @Date:2016年7月18日 下午5:21:40
	 */
	public static boolean checkTwoMonth(String first,String last){
		boolean b = false;
		if(first!=null && last!=null){
			if(!(first.split("-")[1]).equals((last.split("-")[1]))){//跨月
				b = true;
			}
		}
		return b;
	}
	
	/**
	 * 
	 * daysBetween:得到两个日期相差的天数 
	 * @param early
	 * @param late
	 * @return 
	 * @return :int 
	 * @Creator:wizard
	 * @Date:2016年7月19日 下午1:22:12
	 */
	public static final int daysBetween(Date early, Date late) { 
	     
        Calendar calst = Calendar.getInstance();
        Calendar caled = Calendar.getInstance();
        calst.setTime(early);
         caled.setTime(late);
         //设置时间为0时
         calst.set(Calendar.HOUR_OF_DAY, 0);
         calst.set(Calendar.MINUTE, 0);
         calst.set(Calendar.SECOND, 0);
         caled.set(Calendar.HOUR_OF_DAY, 0);
         caled.set(Calendar.MINUTE, 0);
         caled.set(Calendar.SECOND, 0);
        //得到两个日期相差的天数   
         int days = ((int) (caled.getTime().getTime() / 1000) - (int) (calst   
                .getTime().getTime() / 1000)) / 3600 / 24;   
         
        return days;   
   } 
	
	/**
	 * 
	 * getYear:(根据时间获取年份).
	 * @param date
	 * @return
	 * @throws Exception 
	 * @return :Integer 
	 * @Creator:wizard
	 * @Date:2016年10月19日 下午3:08:19
	 */
	public static Integer getYear(Date date)throws Exception{
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		Integer year = c.get(Calendar.YEAR);
		return year;
	}
	
	/**
	 * 
	 * getMonth:(根据时间获得月份).
	 * @param date
	 * @return
	 * @throws Exception 
	 * @return :Integer 
	 * @Creator:wizard
	 * @Date:2016年10月19日 下午3:09:30
	 */
	public static Integer getMonth(Date date)throws Exception{
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		Integer month = c.get(Calendar.MONTH) + 1;
		return month;
	}
	
	/**
	 * 
	 * getDay:(根据时间获得天).
	 * @param date
	 * @return
	 * @throws Exception 
	 * @return :Integer 
	 * @Creator:wizard
	 * @Date:2016年10月19日 下午3:15:21
	 */
	public static Integer getDay(Date date)throws Exception{
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		Integer day = c.get(Calendar.DAY_OF_MONTH);
		return day;
	}
	
	/**
	 * 
	 * getWeekOfYear:(根据时间获得第几周).
	 * @param date
	 * @return
	 * @throws Exception 
	 * @return :Integer 
	 * @Creator:wizard
	 * @Date:2016年10月19日 下午3:24:06
	 */
	public static Integer getWeekOfYear(Date date)throws Exception{
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		Integer weekOfYear = c.get(Calendar.WEEK_OF_YEAR);
		return weekOfYear;
	}
	
}